package com.pg.agent.engine.app.controller;

import com.pg.agent.engine.client.dto.AgentExeDTO;
import com.pg.agent.engine.service.service.AIRunService;
import com.pg.agent.engine.service.utils.SessionConnectManager;
import com.pg.base.vo.ResultVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;


/**
 * AI智能体应用服务
 * @author huhuaping
 * @date 2023/10/30 15:56
 */
@Slf4j
@Controller
@RequestMapping("/agent")
public class AiAgentController {

    @Resource
    private AIRunService runService;


    @GetMapping("/chat")
    public String hello(Model model){

        //存储一个字符串
//        model.addAttribute("name","freemarker");
        //stu
 /*       Student student = new Student();
        student.setName("小明");
        student.setAge(18);
        //存储一个对象
        model.addAttribute("stu",student);*/

        return "chat_bot";
    }

    /**
     * 创建SOP会话
     * @return
     */
    @PostMapping("startSOP")
    @ResponseBody
    public ResultVO<AgentExeDTO> startSOP(@RequestBody AgentExeDTO runSopDTO) {
        log.info("receive AiAgent get startSOP=[runSopDTO={}]", runSopDTO);
        AgentExeDTO runSopDTO1 =runService.startSOP(runSopDTO);
        return ResultVO.instance(runSopDTO1) ;
    }

    /**
     * 执行SOP会话-流式
     *
     * @return
     */
    @PostMapping(value = "runSOPStream")
    public void runSOPStream(@RequestBody AgentExeDTO runSopDTO, HttpServletResponse response) {
        log.info("receive AiAgent runSOPStream=[runSopDTO={}]", runSopDTO);
        runService.runSOPStream(runSopDTO, response);
    }


    /**
     * 结束SOP
     *
     * @param runSopDTO
     */
    @PostMapping("endSOP")
    public void endSOP(@RequestBody AgentExeDTO runSopDTO) {
        log.info("receive AiAgent get clearChapAgent=[runSopDTO={}]", runSopDTO);
        runService.endSOP(runSopDTO);
    }


    /**
     * 测试流式会话1
     *
     * @param
     * @return
     * @throws Exception
     */
    @PostMapping("interrupt")
    @ResponseBody
    public ResultVO interrupt(@RequestBody AgentExeDTO runSopDTO) {

        Map<String, AtomicBoolean> atomicIntegerMap = SessionConnectManager.getInstance().getStoryMap();
        AtomicBoolean atomicBoolean = atomicIntegerMap.get(runSopDTO.getSessionId());
        if (atomicBoolean != null) {
            atomicBoolean.set(false);
            boolean flag = true;
            while (flag) {
                Map<String, AtomicBoolean> stopMap = SessionConnectManager.getInstance().getStopMap();
                if (stopMap.containsKey(runSopDTO.getSessionId())) {
                    flag = false;
                    log.info("中断完成");
                    SessionConnectManager.getInstance().removeStopSessionId(runSopDTO.getSessionId());
                }
            }
            return ResultVO.instance();
        }

        return ResultVO.instance(1000,"停止回答出错");
    }

}
